package ua.pp.parkee.t2;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * Created by parkee on 05.06.14.
 */
public class Consumer<E> implements Runnable {

    private List<E> elements;
    private CircularBuffer<E> circularBuffer;
    private Random random;

    public Consumer(CircularBuffer<E> circularBuffer) {
        elements = new LinkedList<E>();
        this.circularBuffer = circularBuffer;
        random = new Random();
        //System.out.println("Consumer initialized");
    }

    @Override
    public void run() {
        //System.out.println("Consumer run()");
        while (!Thread.interrupted()) {
            E element = circularBuffer.get();
            elements.add(element);
            //System.out.println("Consumed: " + element);
            try {
                Thread.sleep(random.nextInt(100));
            } catch (InterruptedException e) {
                //Ignore
            }
        }
    }
}
